﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ForumsBrowser.BusinessObjects;
using Timer = System.Threading.Timer;
using System.Drawing;

namespace ForumsBrowser.ObjectGateway
{
    public static class Gateway
    {
        private static EventHandler<ForumUpdateEventArgs> _update;
        private static readonly object m_lock = new object();

        public static event EventHandler<ForumUpdateEventArgs> ForumUpdate
        {
            add
            {
                lock (m_lock)
                {
                    _update -= value; 
                    _update += value;
                }
            }
            remove
            {
                lock (m_lock)
                {
                    _update += value;
                }
            }
        }

        public static Thread ThreadGet(string url)
        {
            return ThreadGetAction.Execute(url);
        }

        public static Forum ForumGet(string forumName, DateTime? since)
        {
            return ForumGetAction.Execute(forumName, since);
        }

        public static List<ForumCategory> AvailableForumsGet(bool forcerefresh)
        {
            return AvailableForumsGetAction.Execute(forcerefresh); 
        }

        public static Image ImageGet(string url)
        {
            return GetImageAction.Execute(url);
        }

        public static void ForumSetSelected(string shortForumName, bool selected)
        {
            ForumSetSelectedAction.Execute(shortForumName, selected);
        }

        public static void ForumEnableUpdates(string forumName, EventHandler<ForumUpdateEventArgs> del)
        {
            ForumUpdate += del;
            var updater = new ForumUpdater(forumName, _update);
        }

        public static void ForumDisableUpdates(string forumName)
        {
            ForumUpdater.StopUpdates(forumName);
        }
    }
}
